<?php
/**
 * 职位管理
* @auth andycao
*/
apf_require_class("Bll_BLL");
apf_require_class('Const_BaseInfo');
class Bll_Hr_OutInterface extends BLL{

    private $_relation_bll =null;
    //user bll
    private $_user_bll = null;
    private $_dep_bll = null;
    private $_job_bll = null;
    private $_prop_bll = null;
    private static $instance = NULL;
    public static function get_instance(){
        if(self::$instance == NULL){
            self::$instance = new Bll_Hr_OutInterface();
        }
        return self::$instance;;
    }
    /**
     * @return Bll_Hr_User
     */
    private function get_user_bll(){
        if(!isset($this->_user_bll)){
            apf_require_class("Bll_Hr_User");
            $this->_user_bll = new Bll_Hr_User();
        }
        return $this->_user_bll;
    }
    /**
     * @return Bll_Hr_UserRelation
     */
    private function get_relation_bll(){
        if(!isset($this->_relation_bll)){
            apf_require_class("Bll_Hr_UserRelation");
            $this->_relation_bll = new Bll_Hr_UserRelation();
        }
        return $this->_relation_bll;
    }
    /**
     * @return Bll_Hr_Job
     */
    private function get_job_bll(){
        if(!isset($this->_job_bll)){
            apf_require_class("Bll_Hr_Job");
            $this->_job_bll = new Bll_Hr_Job();
        }
        return $this->_job_bll;
    }
    /**
     * @return Bll_Hr_Department
     */
    private function get_dep_bll(){
        if(!isset($this->_dep_bll)){
            apf_require_class("Bll_Hr_Department");
            $this->_dep_bll = new Bll_Hr_Department();
        }
        return $this->_dep_bll;
    }
    /**
     * @return Bll_Hr_Property
     */
    private function _get_property_bll(){
        if(!$this->_prop_bll){
            apf_require_class("Bll_Hr_Property");
            $this->_prop_bll = new Bll_Hr_Property();
        }
        return $this->_prop_bll;
    }
    /**
     * 用户类型
     */
    public function get_all_user_type(){
        return $this->_get_property_bll()->get_all_user_type();
    }
    /**
     * 用户M级别
     */
    public function get_m_degree(){
        return $this->_get_property_bll()->get_m_degree();
    }
    /**
     * 用户P级别
     */
    public function get_p_degree(){
        return $this->_get_property_bll()->get_p_degree();
    }
    /**
     * 签约城市列表
     * @param boolean $param
     */
    public function get_city_list($param = true){
        return $this->_get_property_bll()->get_city_list($param);
    }
    /**
     * 获取事业部
     */
    public function get_business_list(){
       return $this->_get_property_bll()->get_business_list();
    }
    /**
     * 支持员工姓名、员工号、邮箱
     * @param string $keyword
     * @param $column name|code|email|user_id
     * @return array
     */
    public function search_user($keyword,$columns,$superior,$subordinate){
        $data = array();
        if(empty($keyword) || empty($columns)){
            return $data;
        }

        foreach ($columns as $key => $column) {
            if($column == 'code') {
                $_where[]= " user_code = '{$keyword}' ";
            } elseif ($column == 'name') {
                $_where[]= " user_name like '%".$keyword."%' ";
            } elseif ($column == 'email') {
                $_where[]= " office_mail like '%".$keyword."%' ";
            } elseif ($column == 'domain_account') {
                    $_where[]= " domain_account = '{$keyword}' ";
            } elseif ($column == 'user_id') {
                $_where = " user_id = '{$keyword}' ";
                break;
            }
        }
        if(is_array($_where)){
            $_where = implode(' OR ',$_where);
        }
        if($column=='domain_account'){
            $_where.=' AND job_status=1';
        }
        $userdata =  $this->get_user_bll()->get_user_by_condition($_where);

        if(empty($userdata)){
            return $data;
        }
        $uids = array_keys($userdata);
        $job_data = $this->get_relation_bll()->get_user_job_info($uids);

        $bu_list = Const_BaseInfo::get_business_list();
        $city_list = APF::get_instance()->get_config('oa_cities');
        //format data
        foreach($userdata as $uid=>$val){
            $tel=$val['office_phone'];
            if(!empty($val['office_phone_ex'])){
                $tel.='-'.$val['office_phone_ex'];
            }
            $data[$uid]['user_id'] = $val['user_id'];
            $data[$uid]['name'] = $val['user_name'];//系统用名
            $data[$uid]['real_name'] = $val['real_name'];//身份证名
            $data[$uid]['english_name'] = $val['english_name'];
            $data[$uid]['code'] = $val['user_code'];
            $data[$uid]['email'] = $val['office_mail'];
            $data[$uid]['mobile'] = $val['cellphone'];
            $data[$uid]['office_phone'] = $tel;//座机
            $data[$uid]['join_keep'] = $this->get_user_join_keep($val['enter_date']);//服务年限
            $data[$uid]['job_status'] = $val['job_status'];//是否在职
            $data[$uid]['enter_date'] = $this->_build_date($val['enter_date']);
            $data[$uid]['indue_date'] = $this->_build_date($val['indue_date']);
            $data[$uid]['dimission_date'] = $this->_build_date($val['leaving_date']);

            //职位信息
            $data[$uid]['work_city_id'] = $val['work_city_id'];
            $data[$uid]['city_name'] = $city_list[$val['work_city_id']];

            if(!empty($job_data[$uid]['dep_info'])){
                $job_info = $job_data[$uid]['dep_info'];
                $data[$uid]['bu_name'] = $bu_list[$job_info['bu_id']]['bu_name'];
                $data[$uid]['department_name'] = $bu_list[$job_info['bu_id']]['bu_name'].$job_info['department_name'];
                $data[$uid]['job_name'] = $job_data[$uid]['job_info']['position_name'];
            }else{
                $data[$uid]['bu_name'] ='';
                $data[$uid]['department_name'] = '';
                $data[$uid]['job_name'] = '';
            }
            $data[$uid]['bu_name'] = $bu_list[$job_info['bu_id']]['bu_name'];
            $data[$uid]['department_name'] = $bu_list[$job_info['bu_id']]['bu_name'].$job_info['department_name'];
            $data[$uid]['job_name'] = $job_data[$val['user_id']]['job_info']['position_name'];
            $data[$uid]['bank_name'] = $val['bank_name'];
            $data[$uid]['bank_card'] = $val['bank_card'];
            //$city_id = $job_data[$val['user_id']]['dep_info']['city_id'];
            if($superior) {
                $data[$uid]['superior'] = $this->get_under_leader($val['user_id']);
            }
            if($subordinate) {
                $data[$uid]['subordinate'] = $this->get_subordinate($val['user_id']);
            }
        }
        return $data;
    }
    /**
     * 根据员工入职日期计算累计服务年限
     * @param int $enter_date 20130101
     */
    public function get_user_join_keep($enter_date){
        $join_keep = 0;
        if(empty($enter_date)){
            return $join_keep;
        }
        $time_length = time() - strtotime($enter_date);
        if($time_length >0 ){
            $t = $time_length / (365*24*60*60);
            $join_keep = round($t, 2);
        }else{
            $join_keep =0;
        }
        return $join_keep;
    }
    /**
     * 获取所有在职用户的邮箱和域帐号
     */
    public function get_all_mail_account(){
        return $this->get_user_bll()->get_all_mail_account();
    }
    /**
     * 员工转岗，更新上下几关系
     *
     * @param unknown_type $parent_id
     * @param unknown_type $old_job_id
     * @param unknown_type $job_id
     */
    public function update_relation ($user_id,$new_parent_id, $old_job_id, $job_id)
    {
        if (empty($user_id) || empty($new_parent_id) || empty($old_job_id) || !isset($job_id)) {
            return false;
        }
        if($job_id){//如果传新的job，则更新work和下属
            //更新新work
            $wre = $this->get_relation_bll()->update_work_from_portal($user_id,$old_job_id,$job_id);
            if(!$wre){
                return false;
            }
            //更新下属关系（带到新职位）
            $sre = $this->get_relation_bll()->update_sub_relation ($user_id, $old_job_id, $job_id);
            if(!$sre){
                return false;
            }
        }else{//如果没设置新job则只更新上级关系
            $job_id = $old_job_id;//设置新ID等于老ID
        }
        //更新上级关系
        $pre = $this->get_relation_bll()->update_user_relation_jobid($user_id,$old_job_id,$job_id,$new_parent_id);
        if(!$pre){
            return false;
        }

        return true;
    }
    /**
     * 获取直属上级领导
     * @param int $uid
     */
    public function get_under_leader($uid){
        $data = array();
        if(empty($uid)){
            return $data;
        }
        $job_data = $this->get_user_bll()->get_user_work($uid);
        if(empty($job_data)){
            return $data;
        }
        $relation_data = $this->get_relation_bll()->get_user_parent($uid, $job_data['job_id']);

        if(empty($relation_data)){
            return $data;
        }
        $uids = array();
        foreach($relation_data as $val){
            $uids[] = $val['parent_id'];
        }
        $uids = array_unique($uids);
        $job_data = $this->get_relation_bll()->get_user_job_info($uids);

        $bu_list = Const_BaseInfo::get_business_list();
        $city_list = APF::get_instance()->get_config('oa_cities');
        //user data
        $user_data = $this->get_user_bll()->get_user_by_ids($uids);

        foreach($user_data as $val){
            $data['user_id'] = $val['user_id'];
            $data['mail'] = $val['office_mail'];
            $data['name'] = $val['user_name'];
            $data['code'] = $val['user_code'];
            $data['work_city_id'] = $val['work_city_id'];
            $data['city_name'] = $city_list[$val['work_city_id']];
            if(!empty($job_data[$val['user_id']]['dep_info'])){
                $job_info = $job_data[$val['user_id']]['dep_info'];
                $data['bu_name'] = $bu_list[$job_info['bu_id']]['bu_name'];
                $data['department_name'] = $bu_list[$job_info['bu_id']]['bu_name'].$job_info['department_name'];
                $data['job_name'] = $job_data[$val['user_id']]['job_info']['position_name'];
            }else{
                $data['bu_name'] ='';
                $data['department_name'] = '';
                $data['job_name'] = '';
            }
        }
        return $data;
    }
    /**
     * 获取下属信息
     * @param int $uid
     */
    public function get_subordinate($uid){
        $data = array();
        if(empty($uid)){
            return $data;
        }
        $job_data = $this->get_user_bll()->get_user_work($uid);
        if(empty($job_data)){
            return $data;
        }
        $relation_data = $this->get_relation_bll()->get_user_sub($uid, $job_data['job_id']);
        if(empty($relation_data)){
            return $data;
        }
        $uids = array();
        foreach($relation_data as $val){
            $uids[] = $val['user_id'];
        }
        $uids = array_unique($uids);
        //user data
        $user_data = $this->get_user_bll()->get_user_by_ids($uids);

        foreach($user_data as $val){
            $data[$val['user_id']]['user_id'] = $val['user_id'];
            $data[$val['user_id']]['email'] = $val['office_mail'];
            $data[$val['user_id']]['name'] = $val['user_name'];
            $data[$val['user_id']]['code'] = $val['user_code'];
        }
        return $data;

    }
    /**
     * 获取所有部门
     * @return unknown
     */
    public function search_department(){
        $dep_list = $this->get_dep_bll()->find_where(array());
        $dep_list = $this->get_dep_bll()->build_data($dep_list);
        return $dep_list;
    }
    /**
     * 获取所有User
     */
    public function search_all_user(){
        $_where = array();
        $userdata =  $this->get_user_bll()->get_all_user_ids();
        return $userdata;

    }
    /**
     * 获取员工基本信息包含上级工号
     * @param array $uids
     * @param int $get_job
     * @return multitype:
     */
    public function get_user_by_ids($uids,$get_job=1){
        $data = array();
        if(empty($uids)){
            return $data;
        }
        $userdata = $this->get_user_bll()->get_user_by_ids($uids);
        if(empty($userdata)){
            return $data;
        }
        foreach($userdata as $val){
            $tel=$val['office_phone'];
            if(!empty($val['office_phone_ex'])){
                $tel.='-'.$val['office_phone_ex'];
            }
            $data[$val['user_id']]=array(
                    'user_id'=>$val['user_id'],
                    'name'=>$val['user_name'],
                    'code'=>$val['user_code'],
                    'email'=>$val['office_mail'],
                    'english_name'=>$val['english_name'],
                    'sex'=>$val['sex'],
                    'telphone'=>$tel,
                    'mobile'=>$val['cellphone'],
                    'enter_date'=>$val['enter_date'],
                    'indue_date'=>$val['indue_date'],
                    'dimission_date'=>$val['leaving_date'],
                    'employees_type'=>$val['user_type'],
                    'job_status'=>$val['job_status'],
                    'leaving_type'=>$val['leaving_type'],
                    'last_date'=>$val['last_date'],

                    'job_id'=>'',
                    'job_name'=>'',
                    'position_name'=>'',
                    'department_id'=>'',
                    'department_name'=>'',
                    'city_name'=>'',
                    'bu_name'=>'',
                    'superior_code'=>'',
             );
        }
        if($get_job==1){
            $workdata = $this->get_user_bll()->search_user_work_info($uids);
            foreach($workdata as $val){
                $data[$val['user_id']]['job_id'] = $val['job_id'];
                $data[$val['user_id']]['job_name'] = $val['position_name'];
                $data[$val['user_id']]['department_id'] = $val['department_id'];
                $data[$val['user_id']]['department_name'] = $val['bu_name'].$val['department_name'];
                $data[$val['user_id']]['city_name'] = $val['city_name'];
                $data[$val['user_id']]['bu_name'] = $val['bu_name'];
                $data[$val['user_id']]['superior_code'] = $val['superior_code'];
            }
        }
        return $data;
    }
    /**
     * 遍历等级关系
     */
    public function get_user_relation($uids){
        $st = $this->get_relation_bll()->get_user_relation($uids);
        return $st;
    }
    /**
     * 取出各部门,查找部门管理人
     */
    public function get_department_manage($depid){
        $st = $this->get_relation_bll()->get_department_manager($depid);
        return $st;
    }
    /**
     * 批量获取职位信息
     * @param unknown_type $job_ids
     */
    public function get_job_dep_infos($job_ids){
        $job_list = $this->get_job_bll()->find_by_job_ids($job_ids,3);
        $t_depids = array();

        foreach($job_list as $val){
            $t_depids[$val['job_id']]=$val['department_id'];
        }
        //部门
        $depids = array_values($t_depids);
        $depids = array_unique($depids);
        $dep_list = $this->get_dep_bll()->get_department_by_ids($depids,3);

        $bll = new Bll_Hr_Property();
        $business = $bll->get_business_list();
        $citylist = $bll->get_sort_city_list();

        foreach($job_list as $job_id=>$val){
            $dep_id = $t_depids[$job_id];
            $job_list[$job_id]['department_id'] = $dep_id;
            $job_list[$job_id]['department_name'] = $dep_list[$dep_id]['department_name'];
            $job_list[$job_id]['bu_name'] = $business[$dep_list[$dep_id]['bu_id']]['bu_name'];
            $job_list[$job_id]['job_name'] = $val['position_name'];
            $job_list[$job_id]['city_id'] = $dep_list[$dep_id]['city_id'];
            $job_list[$job_id]['city_name'] = $citylist[$dep_list[$dep_id]['city_id']];
        }
        return $job_list;
    }
    /**
     * 獲取職位，及部門信息
     */
    public function get_job_dep_info($job_id){
        return $this->get_job_bll()->get_job_dep_info($job_id);
    }
    /**
     * 獲取用戶工號
     */
    public function get_user_code($user_type,$city_id,$only_get = true){
        return $this->get_user_bll()->get_user_code($user_type,$city_id,$only_get);
    }
    /**
     * 员工入职，同步数据
     * @return user_id
     */
    public function user_insert($user){
        $user['created'] = time();
        return $this->get_user_bll()->add_user_from_portal($user);
    }
    /**
     * 员工入职，更新数据
     */
    public function user_update($user_id,$data){
        return $this->get_user_bll()->update_user_from_portal($user_id,$data);
    }
    /**
     * 员工入职，新建汇报关系
     */
    public function work_insert($work){
        return $this->get_user_bll()->add_work_from_portal($work);
    }
    /**
     * 取消员工入职，删除汇报关系
     */
    public function relation_delete($work){
        if(!$work['user_id']){
            return 0;
        }
        $data = array('last_update'=>$work['last_update']);
        if(isset($relation['is_deleted'])){
            $data['is_deleted'] = $work['is_deleted'];
        }
        $where = array(
                'user_id'=>$work['user_id'],
                );
        return $this->get_user_bll()->delete_work_from_portal($data,$where);
    }
    /**
     * 员工入职，新建工作关系
     */
    public function relation_insert($relation){
        return $this->get_relation_bll()->add_relation_from_portal($relation);
    }
    /**
     * 取消员工入职，删除工作关系
     */
    public function work_delete($relation){
        if(!$relation['user_id']){
            return 0;
        }
        $data = array('last_update'=>$relation['last_update']);
        if(isset($relation['is_deleted'])){
            $data['is_deleted'] = $relation['is_deleted'];
        }
        $where = array(
                'user_id'=>$relation['user_id'],
        );
        return $this->get_relation_bll()->delete_relation_from_portal($data,$where);
    }
    /**
     * 检测是否为技术部
     * @param int $department_id
     * @return boolean
     */
    public function check_is_tech($department_id){
        $dep=$this->get_dep_bll()->find_dep_by_id($department_id);
        $status = false;
        if($dep['function_id']==6){
            $status = true;
        }
        return $status;
    }
    /**
     * 找出原员工下级uid，并建立与新的员工的汇报关系
     * @param int $old_uid
     * @param int $new_uid
     */
    public function update_user_relation($old_uid,$new_uid){
        $status = false;
        if(empty($old_uid) || empty($new_uid)) {
            return $status;
        }
        //先解除原有关系，再建立新的关系
        $uids = array($old_uid,$new_uid);
        $work_data = $this->get_relation_bll()->get_job_by_uids($uids);
        if(empty($work_data)){
            return $status;
        }

        foreach($work_data as $val){
            $work[$val['user_id']] = $val['job_id'];
        }

        //有一个人没有找到职位信息，退出
        if(empty($work[$old_uid]) || empty($work[$new_uid])){
            return $status;
        }
        //取出原有的下级
        $data = $this->get_relation_bll()->get_user_sub($old_uid,$work[$old_uid]);
        if(empty($data)){
            return $status;
        }
        $ck_uids = array();
        foreach($data as $val){
            $ck_uids[] = $val['user_id'];
        }
        return $this->get_relation_bll()->update_multi_user_parent($ck_uids, $new_uid, $work[$new_uid]);
    }
    /**
     *
     * @param int $time
     */
    private function _build_date($time){
        $date=0;
        if(empty($time)){
            return $date;
        }
        $strtime = strtotime($time);
        $date = date('Y-m-d',$strtime);
        return $date;
    }
    /**
     * 员工离职
     * @param int $uid
     * @param array $data
     */
    public function dimission_user($uid, $params){
        if(empty($uid) || empty($params['leaving_date']) || empty($params['last_date']) || empty($params['leaving_type'])){
            return false;
        }
        $other_reason = empty($params['other_reason']) ? '' : trim($params['other_reason']);
        $data = array(
            'leaving_date'=>intval($params['leaving_date']),
            'last_date'=>intval($params['last_date']),
            'leaving_type'=>intval($params['leaving_type']),
            'reason_id'=>intval($params['reason_id']),
            'other_reason'=>$other_reason
        );
        return $this->get_user_bll()->dimission_user($uid,$data);
    }
    /**
     * 根据工号获取用户信息
     * @param array $user_code
     */
    public function get_user_info_by_code($user_code){
        $result = array();
        if(empty($user_code)){
            return $result;
        }
        $result = $this->get_user_bll()->get_user_by_code($user_code);
        return $result;
    }

    /**
     * 员工基本信息
     * @param array $uid
     * @param int $get_job 是否需要职位信息
     */
    public function get_user_base_info($uid,$get_job=0){
        $data = array();
        if(!$uid){
            return $data;
        }
        $user_data = $this->get_user_bll()->get_user_by_id($uid);
        if(empty($user_data)){
            return $data;
        }
        $data['user_id'] = $user_data['user_id'];
        $data['user_name'] = $user_data['user_name'];
        $data['real_name'] = $user_data['real_name'];
        $data['user_code'] = $user_data['user_code'];
        $data['office_mail'] = $user_data['office_mail'];
        $data['enter_date'] = $user_data['enter_date'];
        $data['p_level'] = $user_data['p_level'];
        $data['m_level'] = $user_data['m_level'];
        $data['domain_account'] = $user_data['domain_account'];
        $data['crm_account'] = $user_data['crm_account'];
        $data['office_phone'] = $user_data['office_phone'];
        $data['office_phone_ex'] = $user_data['office_phone_ex'];
        $data['work_city_id'] = $user_data['work_city_id'];
        //获取职位信息
        if($get_job){
            $workdata = $this->get_user_bll()->get_user_work($uid);
            if(!empty($workdata)){
                $business = $this->_get_property_bll()->get_business_list();
                $citylist = $this->_get_property_bll()->get_sort_city_list();

                $job_id = $workdata['job_id'];
                $job_data = $this->get_job_bll()->get_job_dep_info($job_id);
                $data['job_id'] = $job_id;
                $data['position_name'] = $job_data['position_name'];
                $data['department_id'] = $job_data['department_id'];
                $data['department_name'] = $job_data['department_name'];

                $data['bu_name'] = $business[$job_data['bu_id']]['bu_name'];
                $data['city_name'] = $citylist[$job_data['city_id']];
            }
        }
        $data['user_security_enable'] = empty($_SESSION['user_security_enable']) ? '0':$_SESSION['user_security_enable'];

        $data['system_sa'] = empty($_SESSION['system_sa']) ? '0':$_SESSION['system_sa'];
        return $data;
    }

    /**
     * 用系统密码对数据加密
     * @param unknown_type $value
     */
    public function encrypt_value($value) {
        apf_require_class("Security_Api_Crypt_key");
        $_SESSION['system_sa'] = empty($_SESSION['system_sa']) ? "0":$_SESSION['system_sa'];
        $encrypt_value = Security_Api_Crypt_key::get_instance()->encrypt($value, $_SESSION['system_sa']);
        return $encrypt_value;
    }

    /**
     * 用系统密码对数据解密
     * @param unknown_type $value
     */
    public function decrypt_value($value) {
        apf_require_class("Security_Api_Crypt_key");
        $_SESSION['system_sa'] = empty($_SESSION['system_sa']) ? "0":$_SESSION['system_sa'];
        $encrypt_value = Security_Api_Crypt_key::get_instance()->decrypt($value, $_SESSION['system_sa']);
        return $encrypt_value;
    }
    /**
     * 获取部门下所有职位信息
     * @param int $depid
     */
    public function get_department_jobs($depid){
        $data = array();
        if(!$depid){
            return $data;
        }
        return $this->get_job_bll()->get_job_by_dep($depid);
    }
    /**
     * 职能下有那些部门
     * @param int $function_id
     */
    public function get_bu_by_function($function_id){
        $data = array();
        $_where = array(
                'function_id'=>$function_id
                );
        $dep_list = $this->get_dep_bll()->find_where($_where);
        if(!empty($dep_list)){
            foreach ($dep_list as $val){
                $data[$val['bu_id']] = 1;
            }
        }
        return $data;
    }
    /**
     * 职能部门下有那些城市
     * @param int $function_id
     * @param int $bu_id
     */
    public function get_city_list_by_dep($function_id,$bu_id){
        $data = array();
        $_where = array(
                'function_id'=>$function_id,
                'bu_id'=>$bu_id
        );
        $dep_list = $this->get_dep_bll()->find_where($_where);
        if(!empty($dep_list)){
            foreach ($dep_list as $val){
                $data[$val['city_id']] = 1;
            }
        }
        return $data;
    }
    /**
     * 查询这些员工的直属领导
     * @param array $uids
     */
    public function get_relation_parent($uids){
        return $data=$this->get_relation_bll()->get_user_last_parent($uids);
    }
    /**
     * 通过事业部获取其下的部门
     * @param int $bu_id
     */
    public function get_department_by_business(){
        return $this->get_dep_bll()->find_where(array());
    }
    /**
     *
     */
    public function get_job_list(){
        $_where = array('is_deleted'=>1);
        return $this->get_job_bll()->find_where($_where);
    }
    /**
     * //取职位下所有的人,给招聘端使用，只统计正式和试用的员工人数
     * @param array $job_ids
     */
    public function get_job_user_list($job_ids){
        $result = array();
        if(empty($job_ids)){
            return $result;
        }
        $work_data = $this->get_relation_bll()->get_job_user_list($job_ids);
        $uids = array();
        if(empty($work_data)){
            return $result;
        }
        foreach($work_data as  $val){
            $uids[$val['user_id']] = $val['job_id'];
        }
        $uids_list = array_keys($uids);
        $user_list = $this->get_user_bll()->get_user_by_ids($uids_list);
        if(!empty($user_list)){
            foreach($user_list as $val){
                if($val['job_status']==2){
                    continue;
                }
                $jobid = $uids[$val['user_id']];
                //只统计正式和试用
                if($val['user_type']==Const_User::USER_TYPE_TRY_STAFF || $val['user_type']==Const_User::USER_TYPE_FORMAL_STAFF){
                    $result[$jobid]['fact_num'] +=1;
                }
            }
        }
        return $result;
    }
    /**
     * 取出所有用户后过滤,默认统计销售部
     * @param int $where
     */
    public function get_dep_user_by_function($function_id=9){
        apf_require_class("Bll_Report_Report");
        $dep_bll = new Bll_Report_Report();
        $datalist = $dep_bll->get_all_job_user();
        $data = array();
        $bll = new Bll_Hr_Property();
        $business = $bll->get_business_list();
        $citylist = $bll->get_sort_city_list();
        foreach($datalist as $val){
            $data[$val['bu_id']]['bu_id']=$val['bu_id'];
            $data[$val['bu_id']]['bu_name']=$business[$val['bu_id']]['bu_name'];
            $data[$val['bu_id']]['all_num'] +=1;
            $data[$val['bu_id']]['city_list'][$val['work_city_id']]['all_num'] +=1;
            $data[$val['bu_id']]['city_list'][$val['work_city_id']]['city_name'] = $citylist[$val['work_city_id']];
            if($function_id==$val['function_id']){
                $data[$val['bu_id']]['city_list'][$val['work_city_id']]['sale_num'] +=1;
            }
        }
        return $data;
    }
    /**
     * 搜索离职用户
     * @param int $start 20130301
     * @param int $end 20130301
     * @return multitype:|multitype:multitype:unknown
     */
    public function search_dimission_by_date($start,$end){
        if(empty($start) && empty($end)){
            return array();
        }
        if($start){
            $_where['last_date >='.intval($start).' AND 1=?']=1;
        }
        if($end){
            $_where['last_date <='.intval($end).' AND 1=?']=1;
        }
        $user_data = $this->get_user_bll()->get_user_by_condition($_where,0,0);
        $tmp = array();
        if(!empty($user_data)){
            foreach($user_data as $val){
                $tmp[] = array(
                        'user_id'=>$val['user_id'],
                        'user_code'=>$val['user_code'],
                        'last_date'=>$val['last_date'],
                        'leaving_date'=>$val['leaving_date']
                );
            }
        }
        return $tmp;
    }
    /**
     * 返回所有员工的上下级关系，返回上下级
     * array(
     * 123=>array(
     *     'code' =>SH
     *     'name'=>xx
     *     'enter_date'=>
     *     'superior_code'=>
     *     'superior_uid'=>1
     * )
     *
     * )
     */
    public function build_user_relation(){
        $_where=array('job_status'=>1);
        $user_data = $this->get_user_bll()->get_user_by_condition($_where,0,0);
        $is_direct = 1 ;
        $relation_list = $this->get_relation_bll()->get_all_relation($is_direct);
        //tp($relation_list);
        //格式化数据
        $list = array();
        foreach($relation_list as $val){
            //只有mike没有上级
            if(empty($val['parent_id'])){
                $superior_code = $superior_uid='';
            }else{
                $superior_code = $user_data[$val['parent_id']]['user_code'];
                $superior_uid = $val['parent_id'];
            }
            $list[$val['user_id']] = array(
                                    'user_id'=>$val['user_id'],
                                    'code'=>$user_data[$val['user_id']]['user_code'],
                                    'name'=>$user_data[$val['user_id']]['user_name'],
                                    'user_type'=>$user_data[$val['user_id']]['user_type'],
                                    'enter_date'=>$user_data[$val['user_id']]['enter_date'],
                                    'superior_code'=>$superior_code,
                                    'superior_uid'=>$superior_uid,
                                    );
        }
        //print_r($list[4555]);
        return $list;
    }
}
